<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="geoFilterConfigForm" class="tb-form-panel no-border no-padding">
  <section class="tb-form-panel stroked">
    <div class="tb-form-panel-title" translate>rule-node-config.coordinate-field-names</div>
    <section>
      <div class="flex flex-row gap-4">
        <mat-form-field class="mat-block max-w-50% flex-full">
          <mat-label>{{ 'rule-node-config.latitude-field-name' | translate }}</mat-label>
          <input matInput formControlName="latitudeKeyName" required>
          <mat-error *ngIf="geoFilterConfigForm.get('latitudeKeyName').hasError('required')">
            {{ 'rule-node-config.latitude-field-name-required' | translate }}
          </mat-error>
        </mat-form-field>
        <mat-form-field class="mat-block max-w-50% flex-full">
          <mat-label>{{ 'rule-node-config.longitude-field-name' | translate }}</mat-label>
          <input matInput formControlName="longitudeKeyName" required>
          <mat-error *ngIf="geoFilterConfigForm.get('longitudeKeyName').hasError('required')">
            {{ 'rule-node-config.longitude-field-name-required' | translate }}
          </mat-error>
        </mat-form-field>
      </div>
    <div class="tb-form-hint tb-primary-fill" translate>rule-node-config.coordinate-field-hint</div>
    </section>
  </section>
  <section class="tb-form-panel stroked" [class.no-padding-bottom]="!defaultPaddingEnable">
    <div class="tb-form-panel-title" translate>rule-node-config.geofence-configuration</div>
    <section class="flex flex-col">
      <mat-form-field class="mat-block flex-1" hideRequiredMarker>
        <mat-label>{{ 'rule-node-config.perimeter-type' | translate }}</mat-label>
        <mat-select formControlName="perimeterType">
          <mat-option *ngFor="let type of perimeterTypes" [value]="type">
            {{ perimeterTypeTranslationMap.get(type) | translate }}
          </mat-option>
        </mat-select>
      </mat-form-field>
      <div tb-hint-tooltip-icon="{{ geoFilterConfigForm.get('perimeterType').value === perimeterType.CIRCLE
        ? ('rule-node-config.fetch-circle-parameter-info-from-metadata-hint'
            | translate : { perimeterKeyName: geoFilterConfigForm.get('perimeterKeyName').valid
                              ? geoFilterConfigForm.get('perimeterKeyName').value
                              : 'ss_perimeter'})
        : ('rule-node-config.fetch-poligon-parameter-info-from-metadata-hint'
            | translate : { perimeterKeyName: geoFilterConfigForm.get('perimeterKeyName').valid
                              ? geoFilterConfigForm.get('perimeterKeyName').value
                              : 'ss_perimeter'}) }}"
           class="tb-form-row no-border no-padding slide-toggle">
        <mat-slide-toggle class="mat-slide" formControlName="fetchPerimeterInfoFromMessageMetadata">
          {{ 'rule-node-config.fetch-perimeter-info-from-metadata' | translate }}
        </mat-slide-toggle>
      </div>
      <mat-form-field *ngIf="geoFilterConfigForm.get('fetchPerimeterInfoFromMessageMetadata').value" class="mat-block">
        <mat-label>{{ 'rule-node-config.perimeter-key-name' | translate }}</mat-label>
        <input matInput formControlName="perimeterKeyName" required>
        <mat-error *ngIf="geoFilterConfigForm.get('perimeterKeyName').hasError('required')">
          {{ 'rule-node-config.perimeter-key-name-required' | translate }}
        </mat-error>
        <mat-hint>{{ 'rule-node-config.perimeter-key-name-hint' | translate }}</mat-hint>
      </mat-form-field>
      <div class="flex flex-col"
           *ngIf="geoFilterConfigForm.get('perimeterType').value === perimeterType.CIRCLE &&
                    !geoFilterConfigForm.get('fetchPerimeterInfoFromMessageMetadata').value">
        <div class="flex flex-row gap-4">
          <mat-form-field class="flex-1">
            <mat-label>{{ 'rule-node-config.circle-center-latitude' | translate }}</mat-label>
            <input type="number" min="-90" max="90" step="0.1" matInput formControlName="centerLatitude" required>
            <mat-error *ngIf="geoFilterConfigForm.get('centerLatitude').hasError('required')">
              {{ 'rule-node-config.circle-center-latitude-required' | translate }}
            </mat-error>
          </mat-form-field>
          <mat-form-field class="flex-1">
            <mat-label>{{ 'rule-node-config.circle-center-longitude' | translate }}</mat-label>
            <input type="number" min="-180" max="180" step="0.1" matInput formControlName="centerLongitude" required>
            <mat-error *ngIf="geoFilterConfigForm.get('centerLongitude').hasError('required')">
              {{ 'rule-node-config.circle-center-longitude-required' | translate }}
            </mat-error>
          </mat-form-field>
        </div>
        <div class="flex flex-row gap-4">
          <mat-form-field class="flex-1">
            <mat-label>{{ 'rule-node-config.range' | translate }}</mat-label>
            <input type="number" min="0" step="0.1" matInput formControlName="range" required>
            <mat-error *ngIf="geoFilterConfigForm.get('range').hasError('required')">
              {{ 'rule-node-config.range-required' | translate }}
            </mat-error>
          </mat-form-field>
          <mat-form-field class="flex-1">
            <mat-label>{{ 'rule-node-config.range-units' | translate }}</mat-label>
            <mat-select formControlName="rangeUnit" required>
              <mat-option *ngFor="let type of rangeUnits" [value]="type">
                {{ rangeUnitTranslationMap.get(type) | translate }}
              </mat-option>
            </mat-select>
            <mat-error *ngIf="geoFilterConfigForm.get('rangeUnit').hasError('required')">
              {{ 'rule-node-config.range-units-required' | translate }}
            </mat-error>
          </mat-form-field>
        </div>
      </div>
      <mat-form-field class="mat-block" subscriptSizing="dynamic"
                      *ngIf="geoFilterConfigForm.get('perimeterType').value === perimeterType.POLYGON &&
                         !geoFilterConfigForm.get('fetchPerimeterInfoFromMessageMetadata').value">
        <mat-label>{{ 'rule-node-config.polygon-definition' | translate }}</mat-label>
        <input matInput formControlName="polygonsDefinition" required>
        <mat-hint>{{ 'rule-node-config.polygon-definition-hint' | translate }}</mat-hint>
        <mat-error *ngIf="geoFilterConfigForm.get('polygonsDefinition').hasError('required')">
          {{ 'rule-node-config.polygon-definition-required' | translate }}
        </mat-error>
      </mat-form-field>
    </section>
  </section>
</section>
